package com.example.administrator.yuejian_2.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.yuejian_2.R;
import com.example.administrator.yuejian_2.bean.TrainOrderDetailBean;
import com.example.administrator.yuejian_2.interservice.Const;
import com.example.administrator.yuejian_2.interservice.TrainService;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class TrainOrderActivity extends AppCompatActivity {
    private int trainorder_id;

    private ImageView train_pic;
    private TextView train_name;
    private TextView train_money;
    private TextView train_time;
    private TextView train_address;
    private TextView count;
    private TextView train_count;
    private TextView train_number;
    private TextView trainorder_time;
    private TextView payway;
    private Button tuikuan;
    private Context context;
    private ImageButton back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_train_order);
        trainorder_id = getIntent().getIntExtra("trainorder_id",0);
        initViews();
        getdetail();
    }
    public void initViews(){
        train_pic = (ImageView) findViewById(R.id.train_pic);
        train_name = (TextView) findViewById(R.id.train_name);
        train_money = (TextView) findViewById(R.id.train_money);
        train_time = (TextView) findViewById(R.id.train_time);
        train_address = (TextView) findViewById(R.id.train_address);
        count = (TextView) findViewById(R.id.count);
        train_count = (TextView) findViewById(R.id.train_count);
        train_number = (TextView) findViewById(R.id.train_number);
        trainorder_time = (TextView) findViewById(R.id.trainorder_time);
        payway = (TextView) findViewById(R.id.payway);
        tuikuan = (Button) findViewById(R.id.tuikuan);
        back = (ImageButton) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                finish();
                intent.setClass(TrainOrderActivity.this, TrainDetailActivity.class);
                startActivity(intent);
            }
        });
    }
    public void getdetail(){
        context = TrainOrderActivity.this;
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        TrainService service = retrofit.create(TrainService.class);
        Call<TrainOrderDetailBean> call = service.trainorderdetail(trainorder_id);
        call.enqueue(new Callback<TrainOrderDetailBean>() {
            @Override
            public void onResponse(Call<TrainOrderDetailBean> call, Response<TrainOrderDetailBean> response) {
                if (response.body()!=null){
                    String pic = response.body().getCourse_pic();
                    if (!pic.equals(""))
                        Picasso.with(context)
                                .load("http://10.0.2.2/YueJian16/Public/clubphoto/"+pic)
                                .into(train_pic);
                    train_name.setText(response.body().getCourse_name());
                    train_time.setText(response.body().getCourse_time());
                    train_address.setText(response.body().getCourse_location());
                    String money_one = response.body().getCourse_money();
                    train_money.setText(money_one);
                    int shuliang = response.body().getCount();
                    String s = Integer.toString(shuliang);
                    count.setText("×"+s);
                    int number = response.body().getNumber();
                    String n = Integer.toString(number);
                    train_number.setText(n);
                    trainorder_time.setText(response.body().getTime());
                    payway.setText(response.body().getPayway());
                    int money = Integer.parseInt(money_one);
                    int zongjia = shuliang * money;
                    String total = Integer.toString(zongjia);
                    train_count.setText(total);
                    //tuikuan = (Button) findViewById(R.id.tuikuan);
                }
            }

            @Override
            public void onFailure(Call<TrainOrderDetailBean> call, Throwable t) {

            }
        });
 }
}
